{include file="public/base" /}
{block name="main"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>编辑公告</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <form method="get" action="javascript:;" class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">标题</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="title" value="{$data.title}">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">公告</label>
                            <div class="col-sm-10" >
                                <script id="editor" type="text/plain" style="height:200px;">{$data.content}</script>
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <input type="hidden" id="id" name="id" value="{$data['id']}">
                                <button class="btn btn-primary" type="submit" onclick="edit()">保存内容</button>
                                
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="javascript"}
<script src="ADMIN_JS/content.min.js?v=1.0.0"></script>
<script src="ADMIN_JS/plugins/iCheck/icheck.min.js"></script>

<script>
    var ue = UE.getEditor('editor');
    function edit()
    {
        var formData = new FormData();
        var title = $("[name='title']").val();
        var id = $("[name='id']").val();
        var content = UE.getEditor('editor').getContent() ;
        if(title=='')
        {
            layer.msg('公告标题不能为空')
            return false;
        }
        if(content=='')
        {
            layer.msg('公告内容不能为空')
            return false;
        }
        formData.append("title", title);
        formData.append("content", content);
        formData.append("id", id);
        var url = '{:url("announcement/edit")}';
        var loading = layer.msg('正在提交数据，请稍后...', {icon: 16,time:6000});
        $.ajax({
            type:'post',
            url:url,
            data:formData,
            contentType: false,
            processData: false,
            success:function(data){
                layer.close(loading);
                if(data.code==1){
                    layer.msg(data.message,{icon:6,time:1500});
                    setTimeout(function(){window.parent.location.reload();},1000);
                }else{
                    layer.msg(data.message,{icon:5,time:1500});
                }
            }
        })
    }

</script>
<script type="text/javascript">
    //上传图片预览
    var small_img = document.querySelector('input[name=small_img]');
    var img = document.querySelector('input[name=img]');
    small_imgs = document.querySelector('#small_img');
    imgs = document.querySelector('#img');
    if (small_img) {
        small_img.addEventListener('change', function() {
            var file = this.files[0];
            var reader = new FileReader();
            // 监听reader对象的的onload事件，当图片加载完成时，把base64编码賦值给预览图片
            reader.addEventListener("load", function() {
                small_imgs.src = reader.result;
            }, false);
            // 调用reader.readAsDataURL()方法，把图片转成base64
            reader.readAsDataURL(file);
            $("img").eq(0).css("display", "block");
        }, false);
    }
    if(img){
        img.addEventListener('change', function() {
            var file = this.files[0];
            var reader = new FileReader();
            // 监听reader对象的的onload事件，当图片加载完成时，把base64编码賦值给预览图片
            reader.addEventListener("load", function() {
                imgs.src = reader.result;
            }, false);
            // 调用reader.readAsDataURL()方法，把图片转成base64
            reader.readAsDataURL(file);
            $("img").eq(1).css("display", "block");
        }, false);
    }

</script>
{/block}